package org.seed.quantum.example.udp;

import org.seed.quantum.protocol.StringProtocol;
import org.seed.quantum.protocol.udp.UdpBootstrap;
import org.seed.quantum.socket.AbstractSocketSession;

import java.io.IOException;

public class UdpClientDemo {

    public static void main(String[] args) throws IOException, InterruptedException {
        UdpBootstrap bootstrap = new UdpBootstrap(new StringProtocol(), (session, msg) -> {
            System.out.println("收到服务端消息: " + msg);
        });
        bootstrap.setThreadNum(Runtime.getRuntime().availableProcessors())
                .setReadBufferSize(1024);
        AbstractSocketSession session = bootstrap.open().connect("localhost", 8887);
        byte[] bytes = "hello smart-socket".getBytes();
        for (int i = 0; i < 100; i++) {
            session.writeBuffer().writeInt(bytes.length);
            session.writeBuffer().write(bytes);
            session.writeBuffer().flush();
        }
        Thread.sleep(1000);
        bootstrap.shutdown();
    }
}
